package cn.iocoder.yudao.module.book.controller.app.category;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.book.controller.app.booksShell.vo.AppCollectionsRespVO;
import cn.iocoder.yudao.module.book.controller.app.category.vo.AppCategoryDetailRespVO;
import cn.iocoder.yudao.module.book.controller.app.category.vo.AppLanguageCategoriesRespVO;
import cn.iocoder.yudao.module.book.controller.app.readBook.vo.AppbookImagesRespVO;
import cn.iocoder.yudao.module.book.service.images.bookImagesService;
import cn.iocoder.yudao.module.book.dal.mysql.classify.ClassifyMapper;
import cn.iocoder.yudao.module.book.service.bookshelf.BookshelfService;
import cn.iocoder.yudao.module.book.service.category.CategoryService;
import cn.iocoder.yudao.module.book.service.users.userDTOService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Tag(name = "APP - 分类")
@RestController
@RequestMapping("/api/user/book")
@Validated
@Data
public class AppCategoryController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private BookshelfService bookshelfService;

    @Resource
    private userDTOService userService;

    @Resource
    private ClassifyMapper classifyMapper;
    
    @Resource
    private bookImagesService bookImagesService;

    @GetMapping("/groups")
    @Operation(summary = "获得中英文绘本列表-二级分类")
    public CommonResult<List<AppCollectionsRespVO>> getBookGroups(@Valid @RequestParam("id") Integer id) {
        // 根据大分类ID获取所有绘本集合
        List<AppCollectionsRespVO> allCollectionRespVo = bookshelfService.selectAllCollectionsByClassfiId(id);
        return CommonResult.success(allCollectionRespVo);
    }

    @GetMapping("/details")
    @Operation(summary = "获取分类详情列表（包含绘本集信息）")
    public CommonResult<List<AppCategoryDetailRespVO>> getCategoryDetails() {
        Long userId = userService.getCurrentUserId();
        List<AppCategoryDetailRespVO> categoryDetails = categoryService.getCategoryDetails(userId);
        return CommonResult.success(categoryDetails);
    }

    @GetMapping("/test-details")
    @Operation(summary = "测试分类详情接口")
    public CommonResult<List<AppCategoryDetailRespVO>> testCategoryDetails() {
        // 使用固定用户ID进行测试
        Long userId = 1L;
        List<AppCategoryDetailRespVO> categoryDetails = categoryService.getCategoryDetails(userId);
        return CommonResult.success(categoryDetails);
    }
    
    @GetMapping("/hierarchy")
    @Operation(summary = "根据大分类ID获取层级分类数据")
    public CommonResult<List<AppCategoryDetailRespVO>> getCategoryHierarchy(@Valid @RequestParam("classifyId") Integer classifyId) {
        Long userId = userService.getCurrentUserId();
        List<AppCategoryDetailRespVO> categoryHierarchy = categoryService.getCategoryHierarchyByClassifyId(classifyId, userId);
        return CommonResult.success(categoryHierarchy);
    }
    
    @GetMapping("/test-hierarchy")
    @Operation(summary = "测试层级分类查询接口")
    public CommonResult<List<AppCategoryDetailRespVO>> testCategoryHierarchy(@Valid @RequestParam("classifyId") Integer classifyId) {
        // 使用固定用户ID进行测试
        Long userId = 1L;
        List<AppCategoryDetailRespVO> categoryHierarchy = categoryService.getCategoryHierarchyByClassifyId(classifyId, userId);
        return CommonResult.success(categoryHierarchy);
    }
    
    @GetMapping("/collection-books")
    @Operation(summary = "获取绘本集下的所有图书和图片信息")
    public CommonResult<List<java.util.Map<String, Object>>> getCollectionBooks(@Valid @RequestParam("collectionId") Integer collectionId) {
        List<java.util.Map<String, Object>> booksWithImages = categoryService.getBooksWithImagesByCollectionId(collectionId);
        return CommonResult.success(booksWithImages);
    }
    
    @GetMapping("/categories-hierarchy")
    @Operation(summary = "根据语言参数获取分类层级数据")
    public CommonResult<List<AppLanguageCategoriesRespVO>> getCategoriesHierarchyByLanguage(@Valid @RequestParam("lang") Integer lang) {
        // lang参数：0-中文，1-英文
        List<AppLanguageCategoriesRespVO> categories = categoryService.getCategoriesByLanguage(lang);
        return CommonResult.success(categories);
    }

    @DeleteMapping("/recent/record")
    @Operation(summary = "删除最近阅读记录（按书本ID）")
    public CommonResult<Boolean> deleteRecentRecord(@Valid @RequestParam("bookId") Integer bookId){
        Long userId = userService.getCurrentUserId();
        cn.iocoder.yudao.module.book.dal.mysql.records.RecordsMapper rm =
                cn.iocoder.yudao.framework.common.util.spring.SpringUtils.getApplicationContext()
                        .getBean(cn.iocoder.yudao.module.book.dal.mysql.records.RecordsMapper.class);
        // 软删：reading_records 标记 deleted = true
        cn.iocoder.yudao.module.book.dal.dataobject.records.RecordsDO rec = rm.selectByBookId(bookId, userId);
        if (rec != null) {
            rec.setDeleted(true);
            rm.updateDeleted(rec.getRecordId(), true);
        }
        return CommonResult.success(true);
    }
    
    @GetMapping("/book-images")
    @Operation(summary = "根据书ID获取书的图片信息")
    public CommonResult<List<AppbookImagesRespVO>> getBookImages(@Valid @RequestParam("bookId") Integer bookId) {
        List<AppbookImagesRespVO> bookImages = bookImagesService.getBookImagesByBookId(bookId);
        return CommonResult.success(bookImages);
    }


}